package com.charm.restaurant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.charm.restaurant.common.Result;
import com.charm.restaurant.entity.Inventory;
import com.charm.restaurant.mapper.InventoryMapper;
import com.charm.restaurant.service.InventoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 库存服务实现类
 */
@Slf4j
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {

    /**
     * 新增库存
     */
    @Override
    @Transactional
    public Result<Inventory> saveInventory(Inventory inventory) {
        // 检查是否已存在同名库存
        LambdaQueryWrapper<Inventory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Inventory::getName, inventory.getName());
        Inventory existInventory = this.getOne(queryWrapper);

        if (existInventory != null) {
            return Result.error("已存在同名库存");
        }

        this.save(inventory);
        return Result.success(inventory);
    }

    /**
     * 分页查询库存
     */
    @Override
    public Result<Page<Inventory>> pageInventory(int page, int pageSize, String name, String type) {
        Page<Inventory> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Inventory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null && !name.isEmpty(), Inventory::getName, name);
        queryWrapper.eq(type != null && !type.isEmpty(), Inventory::getType, type);
        queryWrapper.orderByAsc(Inventory::getId); // 按ID升序排序

        this.page(pageInfo, queryWrapper);

        return Result.success(pageInfo);
    }

    /**
     * 根据ID查询库存
     */
    @Override
    public Result<Inventory> getInventoryById(Long id) {
        Inventory inventory = this.getById(id);
        if (inventory == null) {
            return Result.error("库存不存在");
        }
        return Result.success(inventory);
    }

    /**
     * 修改库存
     */
    @Override
    @Transactional
    public Result<Inventory> updateInventory(Inventory inventory) {
        // 检查库存是否存在
        if (this.getById(inventory.getId()) == null) {
            return Result.error("库存不存在");
        }

        this.updateById(inventory);
        return Result.success(inventory);
    }

    /**
     * 删除库存
     */
    @Override
    @Transactional
    public Result<String> deleteInventory(List<Long> ids) {
        this.removeByIds(ids);
        return Result.success("删除库存成功");
    }

    /**
     * 更新库存数量
     */
    @Override
    @Transactional
    public Result<String> updateStock(Long id, Integer quantity, String type) {
        Inventory inventory = this.getById(id);
        if (inventory == null) {
            return Result.error("库存不存在");
        }

        Integer currentQuantity = inventory.getQuantity();
        if ("in".equals(type)) {
            // 入库
            inventory.setQuantity(currentQuantity + quantity);
        } else if ("out".equals(type)) {
            // 出库
            if (currentQuantity < quantity) {
                return Result.error("库存不足");
            }
            inventory.setQuantity(currentQuantity - quantity);
        } else {
            return Result.error("操作类型错误");
        }

        this.updateById(inventory);
        return Result.success("库存更新成功");
    }

    /**
     * 查询库存警告（库存不足）
     */
    @Override
    public Result<List<Inventory>> getInventoryWarning() {
        // 查询库存不足的物品，这里假设库存低于10就告警
        LambdaQueryWrapper<Inventory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.lt(Inventory::getQuantity, 10);

        List<Inventory> list = this.list(queryWrapper);
        return Result.success(list);
    }
}